﻿using MiniFox.Data.NativeClient;
using MySqlConnector;
using System;
using System.Data;
using System.Data.Common;

namespace MiniFox.Data.MySql
{
    /// <summary>
    /// 
    /// </summary>
    public class MySqlDatabase : Database
    {
        internal const string PROVIDER_NAME = "MySqlConnector";

        #region 构造
        /// <summary>
        /// 
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        public MySqlDatabase(string connectionString) : this(connectionString, PROVIDER_NAME)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="providerName"></param>
        /// <param name="connectionString">数据库连接字符串</param>
        public MySqlDatabase(string providerName, string connectionString) : base(providerName, connectionString)
        {
        }

        #endregion
        /// <summary>
        /// 
        /// </summary>
        public override string ParameterPrefix
        {
            get { return "@"; }
        }
        /// <summary>
        /// 
        /// </summary>
        protected override DbProviderFactory CreateDbProviderFactory()
        {
            return MySqlConnectorFactory.Instance;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="cmdText"></param>
        /// <param name="cmdType"></param>
        /// <returns></returns>
        public override IDataCommand CreateDataCommand(string name, string cmdText, CommandType cmdType)
        {
            return new MySqlDataCommand(name, cmdText, cmdType, this);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <returns></returns>
        public override IDataOperation<TEntity> CreateDataOperation<TEntity>()
        {
            return new MySqlDataOperation<TEntity>(this);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <returns></returns>
        public override IDataQuery<TEntity> CreateDataQuery<TEntity>()
        {
            return new MySqlDataQuery<TEntity>(this);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <returns></returns>
        public override IObjectQuery<TEntity> CreateObjectQuery<TEntity>()
        {
            return new NativeObjectQuery<TEntity>(this);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbParameter"></param>
        /// <param name="type"></param>
        protected override void SetParameterDbType(DbParameter dbParameter, Type type)
        {
            MySqlDbType dbType;
            if (type == typeof(Boolean))
            {
                dbType = MySqlDbType.Bit;
            }
            else if (type == typeof(Byte))
            {
                dbType = MySqlDbType.Byte;
            }
            else if (type == typeof(SByte))
            {
                dbType = MySqlDbType.Byte;
            }
            else if (type == typeof(Single))
            {
                dbType = MySqlDbType.Float;
            }
            else if (type == typeof(Double))
            {
                dbType = MySqlDbType.Double;
            }
            else if (type == typeof(Int16))
            {
                dbType = MySqlDbType.Int16;
            }
            else if (type == typeof(UInt16))
            {
                dbType = MySqlDbType.Int16;
            }
            else if (type == typeof(Int32))
            {
                dbType = MySqlDbType.Int32;
            }
            else if (type == typeof(UInt32))
            {
                dbType = MySqlDbType.Int32;
            }
            else if (type == typeof(Int64))
            {
                dbType = MySqlDbType.Int64;
            }
            else if (type == typeof(UInt64))
            {
                dbType = MySqlDbType.Int64;
            }
            else if (type == typeof(Char))
            {
                dbType = MySqlDbType.String;
            }
            else if (type == typeof(String))
            {
                dbType = MySqlDbType.VarString;
            }
            else if (type == typeof(Decimal))
            {
                dbType = MySqlDbType.Decimal;
            }
            else if (type == typeof(DateTime))
            {
                dbType = MySqlDbType.DateTime;
            }
            else if (type == typeof(DateTimeOffset))
            {
                dbType = MySqlDbType.DateTime;
            }
            else if (type == typeof(Byte[]))
            {
                dbType = MySqlDbType.LongBlob;
            }
            else if (type == typeof(Guid))
            {
                dbType = MySqlDbType.Guid;
            }
            else
            {
                dbType = MySqlDbType.JSON;
            }
            ((MySqlParameter)dbParameter).MySqlDbType = dbType;
        }
    }
}
